package com.easymin.weixin.controller.web;

import com.easymin.weixin.common.DataForm;
import com.easymin.weixin.common.DataGrid;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxApplicationManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("wxApplications")
public class WxApplicationController {

  @Autowired
  private WxApplicationManager wxApplicationManager;

  @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
  public String index() {

    return "wxApplication/list";
  }

  @RequestMapping(value = {"datagrid"}, method = RequestMethod.GET)
  public @ResponseBody DataGrid<WxApplication> list(@RequestParam("page") int page,
      @RequestParam("rows") int rows) {

    return wxApplicationManager.findAll(page, rows);
  }

  @RequestMapping(value = "findAll", method = RequestMethod.GET)
  public @ResponseBody List<WxApplication> findAll() {

    return wxApplicationManager.findAll();
  }


  @RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
  public @ResponseBody DataForm<WxApplication> add(WxApplication wxApplication) {
    return wxApplicationManager.insert(wxApplication);
  }

  @RequestMapping(value = "{id}", method = RequestMethod.GET)
  public @ResponseBody WxApplication find(@PathVariable("id") Long id) {

    return wxApplicationManager.fetch(id);
  }

  @RequestMapping(value = "{id}", method = RequestMethod.PATCH)
  public @ResponseBody DataForm<WxApplication> update(
      @ModelAttribute WxApplication wxApplication) {
    
    return wxApplicationManager.updateByPrimaryKey(wxApplication);
  }

  @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
  public @ResponseBody DataForm<WxApplication> delete(@PathVariable("id") Long id) {

    return wxApplicationManager.deleteByPrimaryKey(id);
  }
}
